package com.skytech.component.monitor.enterprise_data_monitoring;


import com.skytech.component.monitor.data.MonitorData;
import com.skytech.component.monitor.util.ElementAbstractUrl;
import com.skytech.component.monitor.util.FormateConnection;
import com.skytech.component.monitor.util.SaveDataUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class NingDongJiDiService implements CrawlerScript {

    private static final String province = "宁东基地";
    @Autowired
    private SaveDataUtil saveDataUtil;


    private static final Logger log = LoggerFactory.getLogger(NingDongJiDiService.class);

    @Override
    public void start() {

        String url = "http://119.60.0.59:20008/hb.portal.enterprise!right.sh?panelMenu=";
        try {
            Document document = FormateConnection.createConnection(Jsoup.connect(url))
                    .get();
            log.info("当前页面--{}", url);
            Elements allElements = document.getAllElements();
            List<String> urlList = ElementAbstractUrl.links(allElements.select("body > table > tbody > tr:nth-child(2) > td > div > dl > dd a"));
            partClickNext(urlList);
        } catch (Exception e) {
            log.error("{}--{}", url, e.getMessage());
        } finally {
            log.info("数据爬取结束");
        }


    }

    private void partClickNext(List<String> urlList) {
        String date = LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        Document document = null;
        for (String url : urlList) {
            try {
                document = FormateConnection.createConnection(Jsoup.connect(url))
                        .data("time", date)
                        .post();
                log.info("当前页面--{}", url);
            } catch (Exception e) {
                log.error(e.getMessage());
            }

            Elements allElements = document.getAllElements();
            String entName = allElements.select("#maincontent > table > tbody > tr:nth-child(1) > td:nth-child(2)").get(0).text();
            if (!allElements.select("#waterTbody tr").isEmpty()) {
                List<MonitorData> monitorDataList = new ArrayList<>();
                allElements.select("#waterTbody tr").forEach(l -> {
                    MonitorData monitorDataCOD = new MonitorData();
                    MonitorData monitorDataAN = new MonitorData();
                    monitorDataCOD.setId(UUID.randomUUID().toString());
                    monitorDataCOD.setProvince(province);
                    monitorDataCOD.setCompanyName(entName);
                    monitorDataCOD.setMonitorInfo("COD");
                    monitorDataCOD.setMonitorPoint(l.getElementsByTag("td").get(0).text());
                    monitorDataCOD.setMonitorTime(l.getElementsByTag("td").get(1).text());
                    monitorDataCOD.setMonitorStandardValue(l.getElementsByTag("td").get(3).text());
                    monitorDataCOD.setOverproof(l.getElementsByTag("td").get(4).text());
                    monitorDataCOD.setMonitorValue(l.getElementsByTag("td").get(2).text() + "," + l.getElementsByTag("td").get(5).text());
                    monitorDataCOD.setCrawlingTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                    monitorDataList.add(monitorDataCOD);


                    monitorDataAN.setId(UUID.randomUUID().toString());
                    monitorDataAN.setProvince(province);
                    monitorDataAN.setCompanyName(entName);
                    monitorDataAN.setMonitorInfo("氨氮");
                    monitorDataAN.setMonitorPoint(l.getElementsByTag("td").get(0).text());
                    monitorDataAN.setMonitorTime(l.getElementsByTag("td").get(1).text());
                    monitorDataAN.setMonitorStandardValue(l.getElementsByTag("td").get(7).text());
                    monitorDataAN.setOverproof(l.getElementsByTag("td").get(8).text());
                    monitorDataAN.setMonitorValue(l.getElementsByTag("td").get(6) + "," + l.getElementsByTag("td").get(9).text());
                    monitorDataAN.setCrawlingTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                    monitorDataList.add(monitorDataAN);


                });

                saveDataUtil.saveTag(monitorDataList, province);
            }


            if (!allElements.select("#gasTbody tr").isEmpty()) {
                List<MonitorData> monitorDataList = new ArrayList<>();

                allElements.select("#gasTbody tr").forEach(l -> {
                    MonitorData monitorDataSO2 = new MonitorData();
                    MonitorData monitorDataNOx = new MonitorData();
                    MonitorData monitorData = new MonitorData();
                    monitorData.setProvince(province);
                    monitorData.setId(UUID.randomUUID().toString());
                    monitorData.setCompanyName(entName);
                    monitorData.setMonitorInfo("烟尘");
                    monitorData.setMonitorPoint(l.getElementsByTag("td").get(0).text());
                    monitorData.setMonitorTime(l.getElementsByTag("td").get(1).text());
                    monitorData.setMonitorValue(l.getElementsByTag("td").get(2).text() + "," + l.getElementsByTag("td").get(5).text());
                    monitorData.setMonitorStandardValue(l.getElementsByTag("td").get(3).text());
                    monitorData.setOverproof(l.getElementsByTag("td").get(4).text());
                    monitorData.setCrawlingTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                    monitorDataList.add(monitorData);

                    monitorDataSO2.setProvince(province);
                    monitorDataSO2.setId(UUID.randomUUID().toString());
                    monitorDataSO2.setCompanyName(entName);
                    monitorDataSO2.setMonitorInfo("SO2");
                    monitorDataSO2.setMonitorPoint(l.getElementsByTag("td").get(0).text());
                    monitorDataSO2.setMonitorTime(l.getElementsByTag("td").get(1).text());
                    monitorDataSO2.setMonitorValue(l.getElementsByTag("td").get(6).text() + "," + l.getElementsByTag("td").get(9).text());
                    monitorDataSO2.setMonitorStandardValue(l.getElementsByTag("td").get(7).text());
                    monitorDataSO2.setOverproof(l.getElementsByTag("td").get(8).text());
                   monitorDataSO2.setCrawlingTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                    monitorDataList.add(monitorDataSO2);

                    monitorDataNOx.setProvince(province);
                    monitorDataNOx.setId(UUID.randomUUID().toString());
                    monitorDataNOx.setCompanyName(entName);
                    monitorDataNOx.setMonitorInfo("NOx");
                    monitorDataNOx.setMonitorPoint(l.getElementsByTag("td").get(0).text());
                    monitorDataNOx.setMonitorTime(l.getElementsByTag("td").get(1).text());
                    monitorDataNOx.setMonitorValue(l.getElementsByTag("td").get(10).text() + "," + l.getElementsByTag("td").get(13).text());
                    monitorDataNOx.setMonitorStandardValue(l.getElementsByTag("td").get(11).text());
                    monitorDataNOx.setOverproof(l.getElementsByTag("td").get(12).text());
                    monitorDataNOx.setCrawlingTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                    monitorDataList.add(monitorDataNOx);


                });
                saveDataUtil.saveTag(monitorDataList, province);
            }

        }
    }


}
